package com.macro.mall.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.mapper.BmsFeaturedStoreLableMapper;
import com.macro.mall.mapper.BmsFeaturedStoreMapper;
import com.macro.mall.mapper.BmsFeaturedStorePicMapper;
import com.macro.mall.model.BmsFeaturedStore;
import com.macro.mall.model.BmsFeaturedStoreLable;
import com.macro.mall.model.BmsFeaturedStorePic;
import com.macro.mall.portal.dto.BmsFeaturedStoreDto;
import com.macro.mall.portal.dto.BmsFeaturedStoreParam;
import com.macro.mall.portal.service.BmsFeaturedStoreService;
import com.macro.mall.portal.util.UserUtils;

import cn.hutool.core.date.DateTime;

import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Service
public class BmsFeaturedStoreServiceImpl implements BmsFeaturedStoreService {
    
    @Autowired
    private UserUtils userUtils;
    
    @Autowired
    private BmsFeaturedStoreMapper bmsFeaturedStoreMapper;
    
    @Autowired
    private BmsFeaturedStoreLableMapper bmsFeaturedStoreLableMapper;
    
    @Autowired
    private BmsFeaturedStorePicMapper bmsFeaturedStorePicMapper;
    
    private static final AtomicLong counter = new AtomicLong();

    @Override
    @Transactional
    public int create(BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        BmsFeaturedStore bmsFeaturedStore = new BmsFeaturedStore(); 
        // 创建唯一id
        long id = generateId();
        
        BeanUtils.copyProperties(bmsFeaturedStoreParam, bmsFeaturedStore);
        Long userId = userUtils.getUserId();
        bmsFeaturedStore.setCreateUser(userId);
        bmsFeaturedStore.setUpdateUser(userId);
        bmsFeaturedStore.setCreateTime(new DateTime());
        bmsFeaturedStore.setUpdateTime(new DateTime());
        bmsFeaturedStore.setId(id);
        int count = bmsFeaturedStoreMapper.insertBmsFeaturedStore(bmsFeaturedStore);
        // 讲解项目-标签
        List<BmsFeaturedStoreLable> bmsFeaturedStoreLableList = bmsFeaturedStoreParam.getBmsFeaturedStoreLableList();
        if (bmsFeaturedStoreLableList.size() > 0) {
            bmsFeaturedStoreLableMapper.insertAll(bmsFeaturedStoreLableList,id);
        }
        // 特色商店-图片
        List<BmsFeaturedStorePic> bmsFeaturedStorePicList = bmsFeaturedStoreParam.getBmsFeaturedStorePicList();
        if (bmsFeaturedStorePicList.size() > 0) {
            bmsFeaturedStorePicMapper.insertAll(bmsFeaturedStorePicList,id);
        }
        return count;
    }

    @Override
    @Transactional
    public int update(BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        BmsFeaturedStore bmsFeaturedStore = new BmsFeaturedStore(); 
        BeanUtils.copyProperties(bmsFeaturedStoreParam, bmsFeaturedStore);
        Long userId = userUtils.getUserId();
        bmsFeaturedStore.setUpdateUser(userId);
        bmsFeaturedStore.setUpdateTime(new DateTime());
        int count = bmsFeaturedStoreMapper.updateBmsFeaturedStore(bmsFeaturedStore);
        
        // 修改的特色商店的id
        Long shopId = bmsFeaturedStoreParam.getId();
        List<Long> ids = new ArrayList<Long>();
        ids.add(shopId);
        
        // 删除讲解项目-标签
        bmsFeaturedStoreLableMapper.delete(ids);
        // 删除特色商店-图片
        bmsFeaturedStorePicMapper.delete(ids);
        
        List<BmsFeaturedStoreLable> bmsFeaturedStoreLableList = bmsFeaturedStoreParam.getBmsFeaturedStoreLableList();
        if (bmsFeaturedStoreLableList.size() > 0) {
            bmsFeaturedStoreLableMapper.insertAll(bmsFeaturedStoreLableList,shopId);
        }
        // 特色商店-图片
        List<BmsFeaturedStorePic> bmsFeaturedStorePicList = bmsFeaturedStoreParam.getBmsFeaturedStorePicList();
        if (bmsFeaturedStorePicList.size() > 0) {
            bmsFeaturedStorePicMapper.insertAll(bmsFeaturedStorePicList,shopId);
        }
        
        return count;
    }

    @Override
    public Map<String,Object> getList(BmsFeaturedStoreParam bmsFeaturedStoreParam) {
        Map<String,Object> resultMap = new HashMap<>();
        List<BmsFeaturedStoreDto> bmsFeaturedStoreDtoList = new ArrayList<>();
        PageHelper.startPage(bmsFeaturedStoreParam.getPageNum(), bmsFeaturedStoreParam.getPageSize());
        String name = bmsFeaturedStoreParam.getName();
        List<BmsFeaturedStore> list = bmsFeaturedStoreMapper.list(name);
        
        PageInfo<BmsFeaturedStore> pageInfo = new PageInfo<BmsFeaturedStore>(list);
        Long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        
        List<BmsFeaturedStorePic> bmsFeaturedStorePicList = bmsFeaturedStorePicMapper.getAll();
        // 获取全部项目-标签
        List<BmsFeaturedStoreLable> bmsFeaturedStoreLableList = bmsFeaturedStoreLableMapper.getAll();
        
        // 特色商店返回值设定
        if (list.size() > 0) {
            for(BmsFeaturedStore bmsFeaturedStore1 : list) {
                
                BmsFeaturedStoreDto bmsFeaturedStoreDto = new BmsFeaturedStoreDto();
                try {
                    Long id = bmsFeaturedStore1.getId();
                    // 设置主体数据
                    BeanUtils.copyProperties(bmsFeaturedStore1, bmsFeaturedStoreDto);
                    // 设置主题当 [是否作为封面为1的场合]
                    List<BmsFeaturedStorePic> resultPic1List = bmsFeaturedStorePicList.stream()
                            .filter(x ->x.getFeaturedStoreId().equals(id) && x.getIsCover())
                            .collect(Collectors.toList());
                    if (resultPic1List.size() > 0) {
                        String pic = resultPic1List.get(0).getPic();
                        bmsFeaturedStoreDto.setPic(pic);
                        bmsFeaturedStoreDto.setCoverPic(pic);
                    }
               
                    List<BmsFeaturedStoreLable> resultLableList = bmsFeaturedStoreLableList.stream().
                            filter(x -> x.getFeaturedStoreId().equals(id)).collect(Collectors.toList());
                    
                    if (resultLableList.size() > 0) {
                        bmsFeaturedStoreDto.setBmsFeaturedStoreLableList(resultLableList);
                    }
                    bmsFeaturedStoreDtoList.add(bmsFeaturedStoreDto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        resultMap.put("pages",pages);
        resultMap.put("total",total);
        resultMap.put("bmsFeaturedStoreDtoList",bmsFeaturedStoreDtoList);

        return resultMap;
    }

    @Override
    public BmsFeaturedStoreDto getItem(Long id) {
        BmsFeaturedStoreDto bmsFeaturedStoreDto = new BmsFeaturedStoreDto();
        
        try {
            // 获取特色商店
            BmsFeaturedStore bmsFeaturedStore = bmsFeaturedStoreMapper.selectById(id);
            // 获取全部商店-图片
            List<BmsFeaturedStorePic> bmsFeaturedStorePicList = bmsFeaturedStorePicMapper.getAll();
            // 获取全部项目-标签
            List<BmsFeaturedStoreLable> bmsFeaturedStoreLableList = bmsFeaturedStoreLableMapper.getAll();

            // 设置主体数据
            BeanUtils.copyProperties(bmsFeaturedStore, bmsFeaturedStoreDto);
            
            // 设置主题当 [是否作为封面为1的场合]
            List<BmsFeaturedStorePic> resultPic1List = bmsFeaturedStorePicList.stream()
                    .filter(x ->x.getFeaturedStoreId().equals(id) && x.getIsCover())
                    .collect(Collectors.toList());
            if (resultPic1List.size() > 0) {
                String pic = resultPic1List.get(0).getPic();
                bmsFeaturedStoreDto.setPic(pic);
                bmsFeaturedStoreDto.setCoverPic(pic);
            }
         // 设置特色商店-图片的返回集合 [是否作为封面为0的场合]
            List<BmsFeaturedStorePic> resultPic0List = bmsFeaturedStorePicList.stream()
                    .filter(x ->x.getFeaturedStoreId().equals(id) && !x.getIsCover())
                    .collect(Collectors.toList());
            if (resultPic0List.size() > 0) {
                bmsFeaturedStoreDto.setBmsFeaturedStorePicList(resultPic0List);
            }
       
            List<BmsFeaturedStoreLable> resultLableList = bmsFeaturedStoreLableList.stream().
                    filter(x -> x.getFeaturedStoreId().equals(id)).collect(Collectors.toList());
            
            if (resultLableList.size() > 0) {
                bmsFeaturedStoreDto.setBmsFeaturedStoreLableList(resultLableList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmsFeaturedStoreDto;
    }

    @Override
    @Transactional
    public int delete(List<Long> ids) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        int count = bmsFeaturedStoreMapper.updateDeleteId(ids,userId);
        // 讲解项目-标签
        bmsFeaturedStoreLableMapper.delete(ids);
        // 特色商店-图片
        bmsFeaturedStorePicMapper.delete(ids);
        return count;
    }
    
    /**
     * 生成唯一id
     * @return
     */
    public static long generateId() {
        long timestamp = System.currentTimeMillis();
        long sequence = counter.incrementAndGet();
        return (timestamp << 16) | (sequence & 0xFFFF); 
    }
}
